﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;

using Might.Infrastructure.Events;
using Might.Infrastructure.Services;
using Might.Infrastructure.DOM;

namespace Might.UIControls.FileSystemBrowser {

    public class FileSystemBrowserPresenter {
        
        private ILoginView loginView;
        private IFileSystemBrowserView fsBrowserView;        
        private IDom domRoot;
        private ILocalFileSystemRoot localFileSystemRoot;
        
        public FileSystemBrowserPresenter() {
            domRoot = CoreServices.Container.Resolve<IDom>();

            domRoot.LoginFinished += new EndProgressEventHandler( domRoot_LoginFinished );
            domRoot.LogoutFinished += new EndProgressEventHandler( domRoot_LogoutFinished );
            
            CoreServices.EventAggregator.GetEvent<ProgressEvent>().Subscribe(ProgressEventsHandler, ThreadOption.UIThread, false);
            
        }              

        void domRoot_LoginFinished( IProgressToken sender , EndProgressEventArgs e ) {                        
            if( e.Result == ProgressResult.Success ) {

                fsBrowserView.IsEnabled = true;
                loginView.Logged = true;                
                
                IDomNode user = domRoot.GetFirstChild( GroupNames.User );
                
                FSBrowserView.AddRoot( user as IUserInfo );

                IDomNodeCollection nodeCollection = user.GetChildren( GroupNames.FileSystemRoot );
                foreach( var node in nodeCollection ) {
                    FSBrowserView.AddItem( user , node );  
                }

                foreach( var node in nodeCollection ) {                    
                    if( node is ILocalFileSystemRoot ) {
                        localFileSystemRoot = node as ILocalFileSystemRoot;
                        localFileSystemRoot.GetChildren( GroupNames.File ).CollectionChanged += new NotifyCollectionChangedEventHandler( localFileSytem_CollectionChanged );
                        break;
                    }                    
                }               
                
            }
            
        }        

        void domRoot_LogoutFinished( IProgressToken sender , EndProgressEventArgs e ) {
            loginView.Logged = false;
            FSBrowserView.Clear();
        }     
        
        public IFileSystemBrowserView FSBrowserView { 
            get { return fsBrowserView; }
            set { 
                fsBrowserView = value;
                fsBrowserView.IsEnabled = false;
                fsBrowserView.FSTreeDoubleClick += new DoubleClickEventHandler(fsBrowserView_FSTreeDoubleClick);
            }            
        }        

        public ILoginView LoginView {
            get { return loginView; }
            set {
                loginView = value;
                loginView.Login += view_Login;
                loginView.LoginAnonymous += view_LoginAnonymous;
                loginView.Logout += view_Logout;
                loginView.Logged = false;
                domRoot.LoginAsAnonymous();
            }
        }

        public void ProgressEventsHandler( IProgressToken progressToken ) {
            
            if( progressToken.EventType == typeof( OpenFileSystemRootEvent ) || progressToken.EventType == typeof( OpenFileEvent ) ) {
                
                if( progressToken.State is EndProgressEventArgs ) {
                    fsBrowserView.IsEnabled = true;
                } else {
                    progressToken.ProgressGo += new ProgressEventHandler( progressToken_ProgressGo );
                }
            
            }
            
        }

        void progressToken_ProgressGo( IProgressToken sender , ProgressEventArgs e ) {            
            if( e is EndProgressEventArgs ) {
                fsBrowserView.IsEnabled = true;
            }            
        }

        private void view_Login(object sender, EventArgs e) {
            // TODO loginView.Name and loginView.Pass should be in eventargs
            domRoot.Login( loginView.Name , loginView.Pass );            
        }

        void view_LoginAnonymous(object sender , EventArgs e) {
            domRoot.LoginAsAnonymous();
        }

        private void view_Logout(object sender, EventArgs e) {               
            domRoot.Logout();            
        }
        
        private void fsBrowserView_FSTreeDoubleClick(object sender, DoubleClickEventArgs e) {                                    
            load( e.DomItem ); 	         	    	    
        }
        
        private void load( IDomNode node ) {

            if( node is IFileSystemRoot ) {
                fsBrowserView.IsEnabled = false;
                if( !( node is ILocalFileSystemRoot ) ) {
                    ( node as IFileSystemRoot ).LoadListFinished += new EndProgressEventHandler( FileSystemBrowserPresenter_LoadListFinished );
                }
                ( node as IFileSystemRoot ).LoadList();
            } else if( node is IFile ) {
                if( !( node is ILocalFile ) ) {
                    IFile file = node as IFile;
                    fsBrowserView.IsEnabled = false;
                    ( node as IFile ).FileSystem.LoadList( node as IFile );
                }
            }
        
        }

        void FileSystemBrowserPresenter_LoadListFinished( IProgressToken sender , EndProgressEventArgs e ) {                                    
            
            fsBrowserView.IsEnabled = true;                        
            if( e.Result == ProgressResult.Success ) {
                IDomNodeCollection col = sender.iDomItem.GetChildren( GroupNames.File );
                foreach( var node in col ) {
                    FSBrowserView.AddItem( sender.iDomItem , node );
                }                                                 
            }            
            
        }       

        void localFileSytem_CollectionChanged( object sender , NotifyCollectionChangedEventArgs e ) {            
            foreach( var node in e.NewItems ) {
                fsBrowserView.AddItem( localFileSystemRoot , node as IDomNode );
            }                        
        }
    }
}
